var app = getApp()
const utils = require('../../utils/util.js')
const gd = getApp().globalData;
const request = utils.wxPromisify(wx.request)
//记录用户访问的次数的文件目录
const visitPath = `${wx.env.USER_DATA_PATH}/visitNum.txt`;
//获取文件管理器
var fs = wx.getFileSystemManager();

Page({
  data: {
    domain: app.globalData.domain,
    swiperIndex: 0,//当前显示图片的位置
    vedioUrl:'',//视频url
    audioUrl:'',//音频url
    hideAudio:false,
    videoNum:'',//显示与隐藏视频的code
    audioNum: '',//显示与隐藏音频的code
    exhibitName:'',
    marginleft: (wx.getSystemInfoSync().windowWidth - 0.48 * wx.getSystemInfoSync().windowHeight) / 2,
    isVideoPlaying: false,
    btn_info: {
      status: false,
      info: "查看介绍视频"
    },
    hiddenmodalput: false

  },


  onLoad: function (options) {
    //这里添加用户浏览展品的次数
    this.readFileUserVisitNum()

    wx.showLoading({
      title: '加载中',
      mask:true
    })
    //动态设置当前页面的标题栏显示文字
    wx.setNavigationBarTitle({
      title: wx.getStorageSync('area').museum_name,
    })
    //判断是通过搜索传值进入还是输入编号或扫码进入
    if (options.museum_id){
      //获取请求参数封装成show对象
      const show = {
        areaNum: options.museum_id,
        toNum: options.exhibit_id,
        picsNum: options.pic_num,
        exhibitName:options.exhibit_name,
        videoNum:options.video_num,
        audioNum:options.audio_num,
      }
      this.initViewData(show)
      wx.setStorageSync('show', show)
    }else{
      //扫码、编号从存储中读取数据
      const show = wx.getStorageSync('show')
      this.initViewData(show)
    }

    
  },
  onReady:function(){
    const hideNum = setInterval(() => {
      wx.hideLoading()
      clearInterval(hideNum)
    }, 1000)
  },
  onUnload:function(){
    if (this.data.audioNum == 1) {
      this.maudio.destroyAudio()
    }
  },
  onShow:function(){
    if (this.data.audioNum == 1) {
      this.maudio = this.selectComponent('#maudio')
      this.maudio.init()
    }
  },
  /**
   * 初始化页面数据
   */
  initViewData:function(show){
    let { areaNum: pavilion_num, toNum: exhibition_id, picsNum: imgNum, exhibitName, videoNum,audioNum } = show
    this.setData({
      audioUrl: gd.domain + gd.respre + pavilion_num + '/mp3/' + exhibition_id + '.mp3',
      vedioUrl: gd.domain + gd.respre + pavilion_num + '/mp4/' + exhibition_id + '.mp4',
      exhibitName,
      videoNum,
      audioNum
    })
    this.setSwiperPic(gd.domain, pavilion_num, imgNum, exhibition_id)
    this.setExhibitionInfo(gd.domain, pavilion_num, exhibition_id)
  },
  bindchange(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },
  /*
  *将轮播图的数据地址传入data
  *@param domian 域名的根路径/we_title
  *@param pavilion_num 展馆号
  *@param pic_nun_pic 该展馆的图片数量映射表
  */
  setSwiperPic: function (domain, pavilion_num, imgNum, id) {
    // console.log(`${domain}    ${pavilion_num}    ${imgNum}   ${id}`)
    let temp = [];
    for (let i = 0; i < imgNum; i++) {
      temp = [...temp, i];
    }
    var swiper_pics = temp.map((item) => {
      return `${domain}sucai/${pavilion_num}/pic/${id}_${item + 1}.jpg`
    })
    this.setData({
      swiper_pics
    })
  },
  //获取展品的介绍
  setExhibitionInfo(domain, pavilion_num, exhibition_id) {
    var that = this;
    wx.request({
      url: `${domain}sucai/${pavilion_num}/txt/${exhibition_id}.txt`,
      success(res) {
        that.setData({
          exhibition_info: utils.getHilightStrArray(res.data.replace(/[\r\n]/g, ""), '####')
        })
      }
    })
  },
  //弹出框的事件
  modalVideo: function () {
    if (this.data.audioNum == 1) {
      this.maudio.pauseAudio()
    }
    this.setData({
      hiddenmodalput: true,
      hideAudio:true
    })
  },
  //关闭按钮  
  closeVideo: function () {
    this.setData({
      hiddenmodalput: false,
      hideAudio: false
    });
  },

  //图片放大预览
  showImage: function(){
    let {swiper_pics,swiperIndex} = this.data
    wx.previewImage({
      current:swiper_pics[swiperIndex],
      urls:swiper_pics,
    })
  },

  //创建并判断是否创建了用户的访问次数
  readFileUserVisitNum: function(){
    console.log(visitPath)
    fs.access({
      path:visitPath,
      success(){
        fs.readFile({
          filePath:visitPath,
          encoding:"utf-8",
          success(res){
            console.log(res.data)
            fs.writeFileSync(visitPath, parseInt(res.data) + 1,"utf-8")
          },
          fail(){
            console.log("读取文件的时候出现问题")
          }
        })
      },
      fail(){
        fs.writeFileSync(visitPath,"1","utf-8");
      }
    })
  }
})
